import 'dart:io';

import 'package:flutter/material.dart';
import 'package:navigator_demo/navigator1pointzero/demo_page.dart';
import 'package:navigator_demo/navigator1pointzero/tab_a.dart';
import 'package:navigator_demo/navigator1pointzero/tab_b.dart';

class ReversePopRouteArguments {
  final List<String> popList;
  ReversePopRouteArguments({this.popList});
}

Route<dynamic> onNaviAGenerateRoute(RouteSettings settings) {
  if (settings.arguments != null &&
      settings.arguments is ReversePopRouteArguments) {
    return PageRouteBuilder(
      pageBuilder: (context, animate1, animate2) {
        Tween<Offset> enter =
            Tween<Offset>(begin: Offset.zero, end: Offset(1.0, 0));
        Tween<Offset> exit =
            Tween<Offset>(begin: Offset(-1.0, 0), end: Offset.zero);
        return SlideTransition(
          position: animate1.drive(enter),
          child: SlideTransition(
            position: animate2.drive(exit),
            child: CommonPage(
              settings: settings,
            ),
          ),
        );
      },
      settings: settings,
    );
  }
  switch (settings.name) {
    case '/':
      return MaterialPageRoute(
        builder: (context) => TabA(),
        settings: settings,
      );
    case '/others':
      return MaterialPageRoute(
        builder: (context) => OthersDemo(),
      );
    default:
      return MaterialPageRoute(
        builder: (context) => CommonPage(
          settings: settings,
        ),
        settings: settings,
      );
  }
}

Route<dynamic> onNaviBGenerateRoute(RouteSettings settings) {
  switch (settings.name) {
    case '/':
      return MaterialPageRoute(
        builder: (context) => TabB(),
        settings: settings,
      );
    default:
      return MaterialPageRoute(
        builder: (context) => CommonPage(
          settings: settings,
        ),
        settings: settings,
      );
  }
}

Route<dynamic> onNaviCGenerateRoute(RouteSettings settings) {
  switch (settings.name) {
    case '/':
      return MaterialPageRoute(
        builder: (context) => TabB(),
        settings: settings,
      );
    default:
      return MaterialPageRoute(
        builder: (context) => CommonPage(
          settings: settings,
        ),
        settings: settings,
      );
  }
}

Route<dynamic> onNaviDGenerateRoute(RouteSettings settings) {
  return MaterialPageRoute(
    builder: (context) => CommonPage(
      settings: settings,
    ),
    settings: settings,
  );
}

class CommonPage extends StatelessWidget {
  final RouteSettings settings;
  final String customData = "customData";
  CommonPage({this.settings});
  @override
  Widget build(BuildContext context) {
    print('====== CommonPage ${settings.name} ====== 3');
    return Scaffold(
      appBar: AppBar(
        leading: _getLeftIconButton(context),
        title: Text('common_page.dart: ${settings.name}'),
      ),
      body: Center(
        child: Text('$settings \n customData:$customData'),
      ),
      floatingActionButton: FloatingActionButton(
          heroTag: UniqueKey(),
          child: Icon(Icons.arrow_forward),
          onPressed: () {
            final nextRouteName =
                String.fromCharCodes([settings.name.codeUnits.first + 1]);
            Navigator.of(context).pushNamed(nextRouteName);
          }),
    );
  }

  Widget _getLeftIconButton(BuildContext context) {
    final icon =
        Platform.isIOS ? Icon(Icons.arrow_back_ios) : Icon(Icons.arrow_back);
    return IconButton(
      icon: icon,
      onPressed: () {
        List<String> popList;
        if (settings.arguments == null) return Navigator.of(context).maybePop();
        if (settings.arguments is List<String>) {
          popList = settings.arguments as List<String>;
        }
        if (settings.arguments is ReversePopRouteArguments) {
          final arugments = settings.arguments as ReversePopRouteArguments;
          popList = arugments.popList;
        }
        if (popList != null) {
          if (popList.isEmpty) {
            return Navigator.of(context).maybePop();
          }
          return Navigator.of(context).pushReplacementNamed(
            popList.last,
            arguments: ReversePopRouteArguments(
              popList: popList..removeLast(),
            ),
          );
        }
      },
    );
  }
}

mixin MultiSubPageRoutesPop on Widget {
  static Object currentRoute;

  void popToRoute(BuildContext context) {
    Navigator.of(context).popUntil(
      ModalRoute.withName('/'),
    );
  }

  void popToAncestorRoute(
    BuildContext context,
    String ancestorName,
    Object arguments,
  ) {
    Navigator.of(context).popAndPushNamed(
      ancestorName,
      result: arguments,
    );
  }
}
